#include <stdio.h>
#include "ofc/DTextScreen.h"

#ifdef HAVE_DTEXTSCREEN 
@interface MyScreen : DTextScreen <DScreenHandler>  // Class for the screen and its events
{
  DColor *fgc;
  DColor *bgc;
}
- (MyScreen *) init;
- (BOOL) open;
- (void) draw :(const char *) key;
- (BOOL) keyPress :(DKey *) key :(int) state;      // Callbacks
- (BOOL) mouseMove :(unsigned) x :(unsigned) y;
- (BOOL) mouseButtonPress :(DKey *) button :(int) state;
- (BOOL) screenResize :(unsigned) maxX :(unsigned) maxY;
- (BOOL) screenFocus :(BOOL) focus;
- (BOOL) screenRedraw :(unsigned) minX :(unsigned) maxX :(unsigned) minY :(unsigned) maxY;
@end

@implementation MyScreen

- (MyScreen *) init
{
  [super init];
  
  fgc = [DColor new];
  bgc = [DColor new];

  return self;
}

- (BOOL) open                          // Open the screen with ..
{
  BOOL ok = NO;

  [fgc textColor :DCLR_YELLOW];        // .. foreground and background color
  [bgc textColor :DCLR_BLUE];
  
  ok = [self open :fgc :bgc];

  if (ok)
  {
    [self screenHandler :self];        // Object is handling its events itself
  }

  return ok;
}

- (void) draw :(const char *) key      // Screen drawing
{
  int maxX = [self maxX];              // Dimensions of screen
  int maxY = [self maxY];

  char hello[] = "Hello World";
  char quit[]  = "Press q to quit";

  [self startDrawing];                 // Start drawing
  [self clear];
  [self drawHLine :1 :1 :maxX-1];      // Draw a some lines
  [self drawHLine :maxX-1 :maxY-1 :1];
                                       // Write some text
  [self cursor :((maxX - strlen(hello))/2) :((maxY-2)/2)];
  [self writeText :hello];

  [self cursor :((maxX - strlen(quit))/2) :((maxY+2)/2)];
  [self writeText :quit];

  if (key != NULL)
  {
    [self cursor :3 :maxY-3];
    [self writeText :"Key pressed:"];
    [self writeText :key];
  }
  [self stopDrawing];
}
                                       // Callback for pressed key
- (BOOL) keyPress :(DKey *) key :(int) state
{
  if ([key key] != 'q')
  {
    DText *info = [key toText];
    
    [self draw :[info cstring]];

    [info free];
  }
  
  return ([key key] != 'q');
}
                                       // Callback for moved mouse
- (BOOL) mouseMove :(unsigned) x :(unsigned) y
{
  return YES;
}
                                       // Callback for pressed mouse button
- (BOOL) mouseButtonPress :(DKey *) button :(int) state
{
  return YES;
}
                                       // Callback for screen resize
- (BOOL) screenResize :(unsigned) maxX :(unsigned) maxY
{
  [self draw :NULL];

  return YES;
}
                                       // Callback for lost and gained screen focus
- (BOOL) screenFocus :(BOOL) focus
{
  [self draw :NULL];

  return YES;
}
                                       // Callback for screen redraw
- (BOOL) screenRedraw :(unsigned) minX :(unsigned) maxX :(unsigned) minY :(unsigned) maxY
{
  [self draw :NULL];

  return YES;
}

@end
#endif

int main(int argc, char *argv[])
{
#ifdef HAVE_DTEXTSCREEN 
  MyScreen *screen = [MyScreen new];

  if ([screen open])                   // Open the screen
  {
    [screen draw :NULL];               // Draw the screen

    [screen waitEvents];               // Wait for and process events
  }
  else
    printf("Could not open the screen..\n");

  [screen free];                       // Cleanup
#else
  printf("DTextScreen is not present in the library.\n");
#endif
  return 0;
}

